package scu.maqiang.mesh.test;

import java.util.Arrays;

import scu.maqiang.fes.FES3T41;
import scu.maqiang.mesh.Mesh3T4;
import scu.maqiang.numeric.CG;
import scu.maqiang.numeric.Triple;

public class LShapeMesh {
	public static void main(String[] args) {
		
//		Mesh2DT3Generator mg = new Mesh2DT3Generator();
//		Mesh2DT3 meshT1 = mg.square2D(4, 4, T3Type.Left);
//		meshT1.scale(0.5, 0.5);
//		Mesh2DT3 meshT2 = mg.square2D(4, 4, T3Type.Right);
//		meshT2.scale(0.5, 0.5);
//		meshT2.translate(0.5, 0.0);
//		Mesh2DT3 meshT3 = mg.square2D(4, 4, T3Type.Right);
//		meshT3.scale(0.5, 0.5);
//		meshT3.translate(0.0, 0.5);
//		meshT1.add(meshT2, meshT3);
//		meshT1.toTecplot("LShapeMeshT.dat");
////		meshT1.displayElements();
//		System.out.println("=======================");
////		meshT1.sortElements();
////		meshT1.displayElements();
////		double[][] coord = new double[3][2];
////		for(int i = 0; i < meshT1.getNt(); i++) {
////			int[] ele = meshT1.getElement(i);
////			meshT1.getCoordPerElement(ele, coord);
////			double[] xyz = meshT1.getinteriorPointInElement(coord);
////			System.out.println(Arrays.toString(ele) + "\t" + Arrays.toString(xyz));
////		}
//		
//		int[] ele = meshT1.pointInMesh(new double[] {0.2, 0.75});
//		System.out.println(Arrays.toString(ele));
//		FES2T31 fs = new FES2T31(meshT1);
//		double[] u = new double[fs.GetNdof()];
//		int[] eleIdx = new int[3];
//		double[] eleVal = new double[3];
//		fs.getValue(u, new double[] {0.2, 0.75}, eleIdx, eleVal);
//		System.out.println(Arrays.toString(eleVal));
//		System.out.println(Arrays.toString(eleIdx));

		Mesh3T4 mesh3 = new Mesh3T4().cube(20, 20, 20);
		mesh3.sortElements();
//		mesh3.displayElements();
//		double[][] coord = new double[4][3];
//		for(int i = 0; i < mesh3.getNt(); i++) {
//			int[] ele = mesh3.getElement(i);
//			mesh3.getCoordPerElement(ele, coord);
//			double[] xyz = mesh3.getinteriorPointInElement(coord);
//			System.out.println(Arrays.toString(ele) + "\t" + Arrays.toString(xyz));
//		}
		
		//mesh3.displayElements();
		Triple<Integer, int[], double[]> ele = mesh3.pointInMesh(new double[] {0.82, 0.75, 0.93}, CG::pointInT4);
		System.out.println(ele.getFirst());
		System.out.println(Arrays.toString(ele.getSecond()));
		System.out.println(Arrays.toString(ele.getThird()));
		System.out.println("================");
		FES3T41 fs = new FES3T41(mesh3);
		double[] u = new double[fs.GetNdof()];
		int[] eleIdx = new int[4];
		double[] eleVal = new double[4];
		fs.getValue(u, new double[] {0.82, 0.75, 0.93}, eleIdx, eleVal);
		System.out.println(Arrays.toString(eleVal));
		System.out.println(Arrays.toString(eleIdx));

		//FinalMeshT.displayNodes();
		//FinalMeshT.displayElements();
		//FinalMeshT.displayBoundarys();
		
		/*
		Mesh2Q4Generator mg2 = new Mesh2Q4Generator();
		Mesh2DQ4 meshQ1 = mg2.square2D(1, 1);
		meshQ1.scale(0.5, 0.5);
		Mesh2DQ4 meshQ2 = mg2.square2D(1, 1);
		meshQ2.scale(0.5, 0.5);
		meshQ2.translate(0.5, 0.0);
		Mesh2DQ4 meshQ3 = mg2.square2D(1, 1);
		meshQ3.scale(0.5, 0.5);
		meshQ3.translate(0.0, 0.5);
		meshQ1.add(meshQ2, meshQ3);
		meshQ1.toTecplot("LShapeMeshQ.dat");
		meshQ1.displayNodes();
		meshQ1.displayElements();
		meshQ1.displayBoundarys();
		*/
		//Mesh2DT3Generator mg = new Mesh2DT3Generator();
		//Mesh2DT3 mesh2D = mg.square2D(60, 60, T3Type.Left);
		//mesh2D.displayNodes();
		//mesh2D.displayElements();
		//mesh2D.displayBoundarys();
		//ScalarFunc chFun = (xy, label, param) -> (xy[0] > 1.0/3 && xy[1] > 1.0/3)? 1.0:-1.0;
		//mesh2D.removeElements(chFun, null, 8);
		//mesh2D.toTecplot("NewMeshByRemoving.dat");

//		Mesh3T4Generator mg = new Mesh3T4Generator();
//		Mesh3T4 mesh3D = mg.cube(256, 256, 256);
//		//mesh3D.displayNodes();
//		//mesh3D.displayElements();
//		//mesh3D.displayBoundarys();
//		int n = 16;
//		double eps = 1.0/n;
//		ScalarFunc chFun = (xy, label, param) -> {
//			double xCell = xy[0] * n - (int)(xy[0] * n);
//			double yCell = xy[1] * n - (int)(xy[1] * n);
//			double zCell = xy[2] * n - (int)(xy[2] * n);
//			return (Math.abs(xCell- 0.5) < 0.25 && Math.abs(yCell - 0.5) < 0.25 && Math.abs(zCell - 0.5) < 0.25)? 1.0:-1.0;	
//		};
//		mesh3D.removeElements(chFun, null, 8);
//		mesh3D.toTecplot("NewMeshByRemoving3D.dat");
	}
}
